<?php

namespace haibao\app\data;

use haibao\app\model\filter\Article as ArticleFilter;

class Article extends \haibao\app\data\BaseMysql{
	
	public static $starTagType = \haibao\app\model\data\Article::STAR_TAG;
	public static $brandTagType = \haibao\app\model\data\Article::BRAND_TAG;
	public static $fashionTagType = \haibao\app\model\data\Article::FASHION_TAG;
	public static $eventTagType = \haibao\app\model\data\Article::EVENT_TAG;
	
	public function __construct(){
		parent::__construct('\haibao\app\model\data\Article');
	}

	public function getArticleByTitle($name){
	    $retArr = array();
	    if($name){
	        $result = $this->query('select Id,Title,Type from Article where Published=%s and Status=%s and Title like %s order by Id desc limit 20', array(
	            \haibao\app\model\data\Article::PUBLISHED_YES, \haibao\app\model\data\Article::STATUS_ENABLE, '%'.$name.'%'
	        ));
	    }else{
	        $result = $this->query('select Id,Title,Type from Article where Published=%s and Status=%s order by Id desc limit 20', array(
	            \haibao\app\model\data\Article::PUBLISHED_YES, \haibao\app\model\data\Article::STATUS_ENABLE
	        ));
	    }
	    while ($row = $result->fetch_assoc()) {
	        array_push($retArr, $row);
	    }
	    return $retArr;
	}
	
	public function getArticleImagesId($articleId){
	    $sql = 'select ImageId from ArticleImage where ArticleId = %s';
	    $result = $this->query($sql,array($articleId));
	    $image = array();
	    while($row = $result->fetch_assoc()){
	        $image[] = $row['ImageId'];
	    }
	    return $image;
	}
	
	public function getArticleImageId($articleId){
		$imageId = '';
		$sql ="select * from `Article` where Id = %s";
		$result = $this->query($sql,array($articleId));
		while($result->fetch_assoc()){
			$imageId = $result->getData('ImageId');
		}
		
		return $imageId;
	}
	
	/**
	 * 查询文章详细信息
	 * @param int $articleId
	 */
	public function getOneArticleById($articleId){
		return $this->query("select a.*,b.ClickNum,b.CommentNum,b.VoteNum,b.ShareNum,b.AppClickNum,b.PcClickNum,b.WapClickNum,b.PushClickNum,b.PraiseNum from `Article` as a left join `ArticleProperty` as b on a.Id=b.ArticleId where a.Id = %s",array($articleId))->fetch_assoc();
	}
	
	/**
	 * 保存文章
	 * @param \haibao\app\model\data\Article $model
	 */
	public function saveArticle($model){
		$this->beginTranscation();
		try {
			if($model->Id){
				$this->save($model);
			} else {
				$this->add($model);
				$this->addArticleProperty($model->Id);
			}
			$this->addArticleColumn($model->Id, $model->Column);
 			$this->addArticleStar($model->Id, $model->Star);
 			$this->addArticleBrand($model->Id, $model->Brand);
 			$this->addArticleFashion($model->Id, $model->Fashion);
 			$this->addArticleArea($model->Id, $model->Area);
 			$this->addArticleEvent($model->Id, $model->Event);
			$this->addArticleVote($model->Id, $model->Vote, $model->IsVote);
  			$this->addArticleImage($model->Id, $model->Image);
  			$this->addArticleRelatedTag($model->Id, self::$starTagType, $model->StarTag);
  			$this->addArticleRelatedTag($model->Id, self::$brandTagType, $model->BrandTag);
  			$this->addArticleRelatedTag($model->Id, self::$fashionTagType, $model->FashionTag);
  			$this->addArticleRelatedTag($model->Id, self::$eventTagType, $model->EventTag);
		} catch (\Exception $e) {
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}
	
	public function addArticlePcToApp($model){
	    $this->beginTranscation();
	    try {
	       $this->add($model);
	       $this->addArticleProperty($model->Id);
	       $this->addArticleColumn($model->Id, $model->Column);
	       $this->addArticleImage($model->Id, $model->Image); 
	    } catch (\Exception $e) {
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}
	
	/**
	 * 预发布/发布中文章id
	 */
	public function getCronArticleId($published){
		$articleId=array();
		if($published == \haibao\app\model\data\Article::PUBLISHED_ING){
			$sql="select Id,Type from Article where Published = %s and Status = 1 and ImageId<>0";
		}else{
			$today = date('Y-m-d H:i:s',time());
			$sql="select Id,Type from Article where Published = %s and Status = 1 and PublishTime<='".$today."' and ImageId<>0";
		}
		$result = $this->query($sql, array($published));
		while($result->fetch_assoc()){
			$articleId[$result->getData('Id')] = $result->getData('Type');
		}
		return $articleId;
	}
	
	/**
	 * 修改文章发布状态
	 * @param Array/int $articleId
	 * @param int $published  0:未发布，1：已经发布，2：预发布
	 */
	public function updateArticlePublish($articleId, $published = \haibao\app\model\data\Article::PUBLISHED_YES) {
		if (is_int ( $articleId )) {
			$sql = "update Article set Published=%s where Id=%s  ";
			$arrValues = array (
				$published,
				$articleId
			);
			$this->query ( $sql, $arrValues );
		} elseif (is_array ( $articleId )) {
			$str = preg_replace('/(\d+)/', '%s', implode(',', $articleId));
			$sql = "update Article set Published=".$published." where Id in ($str)  ";
			$this->query ( $sql, $articleId);
		}
	}
	
	public function getActivityType($activityId){
		$type = \haibao\app\model\data\ActivityProperty::TYPE_ACTIVITY;
		$sql="select Type from ActivityProperty where ArticleId=".$this->escape($activityId);
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$type = $result->getData('Type');
			break;
		}
		return $type;
	}
	
	/**
	 * 修改文章状态 
	 */
	public function updateArticleStatus($articleId, $status = \haibao\app\model\data\Article::STATUS_DISABLE){
		$this->query('update Article set Status=%s where Id=%s limit 1', array(
		    $status, $articleId
		));
	}
	
	/**
	 * 修改文章地域
	 */
	public function updateArticleRegion($region, $articleId){
		$this->query('update Article set RegionId = %s where Id = %s',array($region, $articleId));
	}
	
	/**
	 * 获取文章关联标签
	 */
	public function getRelateType($idArr){
		return $this->query('select Id,NameCN from FashionElement where Id = %s',array($idArr))->fetch_assoc();
	}
	
	/**
	 * APP测试用
	 */
	public function getArticleRand(){
		$sql = 'select Id,Title,ImageId,PublishTime,Type from Article order by rand() limit 1,5';
		$result = $this->query($sql);
		$articleList = array();
		while($result->fetch_assoc()){
			$model = new \haibao\app\model\data\Article();
			$model->Id = $result->getData('Id');
			$model->PublishTime = $result->getData('PublishTime');
			$model->ImageId = $result->getData('ImageId');
			$model->Title = $result->getData('Title');
			$model->Type = $result->getData('Type');
			$articleList[] = $model;
		}
		return $articleList;
	}
	
	/**
	 * 设置资讯点击量
	 * @param unknown_type $articleId
	 * @param int $fromType  来源1APP，2WAP，3PC源
	 */
	public function updateClickNum($articleId,$fromType = \haibao\app\model\data\ArticleProperty::FROM_TYPE_WAP){
		$setStr = '';
		if($fromType == \haibao\app\model\data\ArticleProperty::FROM_TYPE_APP){
			$setStr = ',AppClickNum=AppClickNum+1';
		} elseif($fromType == \haibao\app\model\data\ArticleProperty::FROM_TYPE_WAP) {
			$setStr = ',WapClickNum=WapClickNum+1';
		} elseif($fromType == \haibao\app\model\data\ArticleProperty::FROM_TYPE_PC) {
			$setStr = ',PcClickNum=PcClickNum+1';
		} elseif($fromType == \haibao\app\model\data\ArticleProperty::FROM_TYPE_PUSH) {
			$setStr = ',PushClickNum=PushClickNum+1';
		}
		$sql = "update ArticleProperty set ClickNum=ClickNum+1".$setStr." where ArticleId=%s";
		$result  = $this->query($sql,array($articleId));
	}
	
	public function addShare($articleId,$shareType,$uId){
		$sql = 'update ArticleProperty set ShareNum = ShareNum + 1 where ArticleId=%s';
		$this->query($sql, array($articleId));
		$this->query('insert into `ShareRecord` (ArticleId, ShareType,UId,CreateTime) values (%s,%s,%s,%s)', array($articleId,$shareType,$uId,date('Y-m-d H:i:s',time())));
	}
	
	/**
	 * 是否已经被点赞
	 */
	public function isPraised($userId, $articleId){
	    $result = $this->query('select * from ArticlePraise where ArticleId=%s and UserId=%s', array(
	        $articleId, $userId
	    ))->fetch_assoc();
	    return !empty($result);
	}
	
	/**
	 * 文章点赞
	 */
	public function praiseArticle($userId, $articleId){
	    $this->beginTranscation();
	    try {
	        $this->query('replace into ArticlePraise(ArticleId, UserId, CreateTime) values(%s, %s, %s)', array(
	            $articleId, $userId, date('Y-m-d H:i:s')
	        ));
	        $this->unsetPraiseCount($articleId);
	    }catch(\Exception $e){
			$this->rollback();
			throw $e;
		}
	    $this->endTranscation();
	}
	
	/**
	 * 取消点赞
	 */
	public function deletePraise($userId, $articleId){
	    $this->beginTranscation();
	    try {
	        $this->query('delete from ArticlePraise where ArticleId=%s and UserId=%s limit 1', array(
	            $articleId, $userId
	        ));
	        $this->unsetPraiseCount($articleId);
	    }catch(\Exception $e){
	        $this->rollback();
	        throw $e;
	    }
	    $this->endTranscation();
	}
	
	public function getSimilarArticle($articleId, $columnId = null){
	    $retArr = array();
	    $relTag = $this->query('select TagType,TagId from ArticleRelatedTag where ArticleId=%s limit 1', array($articleId))->fetch_assoc();
	    
	    if($relTag){
    	    $cache = new \haibao\app\cache\Article();
    	    $retArr = $cache->getSimilarArticle($relTag['TagId']);
    	    
    	    if(!$retArr){
        	    if($relTag['TagType'] == \haibao\app\model\data\Article::STAR_TAG){
        	        $sql = 'select a.* from Article as a left join ArticleStar as b on a.Id=b.ArticleId where a.`Status`=1 and a.Published=1 and a.Id!=%s and b.StarId=%s';
        	    }elseif($relTag['TagType'] == \haibao\app\model\data\Article::BRAND_TAG){
        	        $sql = 'select a.* from Article as a left join ArticleBrand as b on a.Id=b.ArticleId where a.`Status`=1 and a.Published=1 and a.Id!=%s and b.BrandId=%s';
        	    }elseif($relTag['TagType'] == \haibao\app\model\data\Article::FASHION_TAG){
        	        $sql = 'select a.* from Article as a left join ArticleFashion as b on a.Id=b.ArticleId where a.`Status`=1 and a.Published=1 and a.Id!=%s and b.FashionId=%s';
        	    }elseif($relTag['TagType'] == \haibao\app\model\data\Article::EVENT_TAG){
        	        $sql = 'select a.* from Article as a left join ArticleEvent as b on a.Id=b.ArticleId where a.`Status`=1 and a.Published=1 and a.Id!=%s and b.EventId=%s';
        	    }
        	    $result = $this->query($sql, array($articleId, $relTag['TagId']));
        	    while ($row = $result->fetch_assoc()){
        	        $retArr[$row['Id']] = $row;
        	    }
        	    $cache->setSimilarArticle($relTag['TagId'], $retArr);
    	    }
	    }
	    
	    if(!$retArr || count($retArr)<3){
            $sql = 'select t.Id,t.Title,t.Content,t.ImageId from Article as t
            left join ArticleStar as t1 on t.Id=t1.ArticleId
            left join ArticleBrand as t2 on t.Id=t2.ArticleId
            left join ArticleFashion as t3 on t.Id=t3.ArticleId
            left join ArticleEvent as t4 on t.Id=t4.ArticleId';
            
            if($columnId && $columnId != null){
                $sql .= ' left join ArticleColumn as t5 on t.Id=t5.ArticleId ';
            }
                
            $sql .= ' where t.`Status`=1 and t.Published=1 and t.Id!=%s';
            
            if($columnId && $columnId != null){
                $sql .= ' and t5.ColumnId='.$columnId;
            }
            
            $sql .= ' and (
            t1.StarId in (select StarId from ArticleStar where ArticleId=%s)
            or
            t2.BrandId in (select BrandId from ArticleBrand where ArticleId=%s)
            or
            t3.FashionId in (select FashionId from ArticleFashion where ArticleId=%s)
            or
            t4.EventId in (select EventId from ArticleEvent where ArticleId=%s)
            )
            group by t.Id
            order by PublishTime desc
            limit 30';
            $result = $this->query($sql, array_pad(array(), 5, $articleId));
            while ($row = $result->fetch_assoc()){
                $retArr[$row['Id']] = $row;
            }
	    }
	    
	    if(!$retArr && $columnId){
	        $result = $this->query('select t.Id,t.Title,t.Content,t.ImageId from Article as t left join ArticleColumn as t1 on t.Id=t1.ArticleId where t1.ColumnId=%s and t.`Status`=1 and t.Published=1 and t.Id!=%s order by PublishTime desc limit 30', 
	            array($columnId, $articleId)
            );
	        while ($row = $result->fetch_assoc()){
	            $retArr[$row['Id']] = $row;
	        }
	    }
	    
	    return $retArr;
	}
	
	/**
	 * 重置文章点赞数量
	 */
	private function unsetPraiseCount($articleId){
	    $data = new \haibao\app\data\ArticleProperty();
	    $data->unsetPraiseCount($articleId);
	}
	
	/**
	 * 添加文章关联分类
	 */
	private function addArticleColumn($articleId, $columnId){
		$this->query('delete from `ArticleColumn` where ArticleId = %s', array(
		    $articleId
		));
		if(!$columnId){
		    return false;
		}
		$this->query('insert into `ArticleColumn` (ArticleId, ColumnId) values (%s,%s)', array(
		    $articleId,
		    $columnId
		));
	}
	
	/**
	 * 添加文章关联明星
	 */
	private function addArticleStar($articleId, $star){
		$this->deleteArticleStar($articleId);
		if(!$star){
		    return false;
		}
		
		$sql = 'insert into ArticleStar(ArticleId, StarId) values ';
		$arrSql = $arrValues = array();
		foreach($star as $starId){
			array_push($arrSql, '(%s,%s)');
			array_push($arrValues, $articleId);
			array_push($arrValues, $starId);
		}
		if($arrSql && $arrValues){
			$sql .= implode(',', $arrSql);
			$this->query($sql, $arrValues);
		}
	}
	
	/**
	 * 添加文章关联品牌
	 * @param int $articleId
	 * @param array $brand
	 */
	private function addArticleBrand($articleId, $brand){
		$this->deleteArticleBrand($articleId);
		if(!$brand){
		    return false;
		}
		
		$sql = 'insert into ArticleBrand(ArticleId,BrandId) values ';
		$arrSql = $arrValues = array();
		foreach($brand as $brandId){
			array_push($arrSql, '(%s,%s)');
			array_push($arrValues, $articleId);
			array_push($arrValues, $brandId);
		}
		if($arrSql && $arrValues){
			$sql .= implode(',', $arrSql);
			$this->query($sql, $arrValues);
		}
	}
	
	/**
	 * 添加文章关联服饰
	 */
	private function addArticleFashion($articleId, $fashion){
		$this->deleteArticleFashion($articleId);
		if(!$fashion){
		    return false;
		}
		
		$sql = 'insert into ArticleFashion(ArticleId,FashionId) values ';
		$arrSql = $arrValues = array();
		foreach($fashion as $fashionId){
			array_push($arrSql, '(%s,%s)');
			array_push($arrValues, $articleId);
			array_push($arrValues, $fashionId);
		}
		if($arrSql && $arrValues){
			$sql .= implode(',', $arrSql);
			$this->query($sql, $arrValues);
		}
	}
	
	/**
	 * 添加文章关联地标
	 */
	private function addArticleArea($articleId, $area){
		$this->deleteArticleArea($articleId);
		if(!$area){
		    return false;
		}
		
		$sql = 'insert into ArticleArea(ArticleId,AreaId) values ';
		$arrSql = $arrValues = array();
		foreach($area as $areaId){
			array_push($arrSql, '(%s,%s)');
			array_push($arrValues, $articleId);
			array_push($arrValues, $areaId);
		}
		if($arrSql && $arrValues){
			$sql .= implode(',', $arrSql);
			$this->query($sql, $arrValues);
		}
	}
	
	/**
	 * 添加文章关联事件
	 */
	private function addArticleEvent($articleId, $event){
		$this->deleteArticleEvent($articleId);
		if(!$event){
		    return false;
		}
		
		$sql = 'insert into ArticleEvent(ArticleId,EventId) values ';
		$arrSql = $arrValues = array();
		foreach($event as $eventId){
			array_push($arrSql, '(%s,%s)');
			array_push($arrValues, $articleId);
			array_push($arrValues, $eventId);
		}
		if($arrSql && $arrValues){
			$sql .= implode(',', $arrSql);
			$this->query($sql, $arrValues);
		}
	}
	
	/**
	 * 添加文章关联投票
	 */
	private function addArticleVote($articleId, $vote, $isVote){
	    if(!$vote || $isVote == ''){
	       $sql = "delete from `ArticleVote` where ArticleId = %s";
	       $this->query($sql, array($articleId));
	    }else {
	    	$voteId = array();
	    	foreach($vote as $key=>$v){
	    		$voteId[]= $key;
	    	}
	    	$voteIds = implode(',', $voteId);
	    	$result = $this->getVoteById($voteIds)->fetch_assoc();
	    	//存在数据
	    	if(!empty($result)){
	    		$oldData = $this->getArticleVote($articleId);
	    		$dbVote = $commonVote = $newVote = $voteIdArr = array();
	    		foreach($oldData as $key=>$v){
	    			$dbVote[$v['Id']] = $v['Content'];
	    		}
	    		if(count($dbVote) == count($vote)){
	    		    foreach($dbVote as $key=>$val){
	    		        if($dbVote[$key] != $vote[$key]){
	    		            $newVote[$key] = $vote[$key];
	    		        }
	    		    }
	    		    $this->deleteVote(array_keys($newVote));
	    		    $vote = $newVote;
	    		}else{
	    		    //共同的部分
	    		    //$commonVote = array_intersect($dbVote, $vote);
	    		    foreach($dbVote as $key=>$val){
	    		        if(isset($vote[$key])){
	    		            $commonVote[$key] = $val;
	    		        }
	    		    }
	    		    //新的数据
	    		    //$newVote = array_diff($vote, $commonVote);
	    		    foreach($vote as $key=>$val){
	    		        if(!isset($commonVote[$key])){
	    		            $newVote[$key] = $vote[$key];
	    		        }
	    		    }
	    		    //删除的数据
	    		    //$voteIdArr = array_diff($dbVote, $commonVote);
	    		    foreach($dbVote as $key=>$val){
	    		        if(!isset($commonVote[$key])){
	    		            $voteIdArr[$key] = $dbVote[$key];
	    		        }
	    		    }
	    		    if($voteIdArr){
	    		        $this->deleteVote(array_keys($voteIdArr));
	    		    }
	    		    //插入新数据
	    		    $vote = $newVote;
	    		}
	    		
	    	}
	    	$this->addVote($articleId, $vote);
	    }
	}
	
	/**
	 * 删除投票
	 */
	private function deleteVote(array $idArr){
	    $str = preg_replace('/(\d+)/', '%s', implode(',', $idArr));
	    $this->query("delete from `ArticleVote` where Id in ($str)", $idArr);
	}
	
	/**
     * 添加投票
	 */
	private function addVote($articleId, $vote){
	    $sql = 'insert into ArticleVote(ArticleId,Content) values ';
	    $arrSql = $arrValues = array();
	    foreach($vote as $content){
	        if(empty($content)) continue;
	        array_push($arrSql, '(%s,%s)');
	        array_push($arrValues, $articleId);
	        array_push($arrValues, $content);
	    }
	    if($arrSql && $arrValues){
	        $sql .= implode(',', $arrSql);
	        $this->query($sql, $arrValues);
	    }
	}
	
	/**
	 * 添加文章关联图片
	 */
	private function addArticleImage($articleId, $image){
		$this->deleteArticleImage($articleId);
		if(!$image){
		    return false;
		}
		
		$sql = 'insert into ArticleImage(ArticleId,ImageId,ImageType) values ';
		$arrSql = $arrValues = array();
		foreach($image as $imageId=>$imageType){
			array_push($arrSql, '(%s,%s,%s)');
			array_push($arrValues, $articleId);
			array_push($arrValues, $imageId);
			array_push($arrValues, $imageType);
		}
		if($arrSql && $arrValues){
			$sql .= implode(',', $arrSql);
			$this->query($sql, $arrValues);
		}
	}
	
	/**
	 * 添加文章关联属性
	 */
	private function addArticleProperty($articleId){
		$this->query('insert into `ArticleProperty` (ArticleId) values (%s)' , array($articleId));
		
	}
	
	/**
	 * 添加文章关联标签
	 */
	private function addArticleRelatedTag($articleId,$tagType,$tag){
		$this->deleteArticleRelatedTag($articleId,$tagType);
		if($tag){
			$sql = 'insert into ArticleRelatedTag(ArticleId,TagType,TagId) values ';
			$arrSql = $arrValues = array();
			foreach($tag as $tagId){
				array_push($arrSql, '(%s,%s,%s)');
				array_push($arrValues, $articleId);
				array_push($arrValues, $tagType);
				array_push($arrValues, $tagId);
			}
			if($arrSql && $arrValues){
				$sql .= implode(',', $arrSql);
				$this->query($sql, $arrValues);
			}
		}
	}
	
	/**
	 * 根据投票ID查询投票记录
	 */
	private function getVoteById($voteId){
		return $this->query('select * from `ArticleVote` where Id in (%s)' , array($voteId));
	}

	/**
	 * 删除文章关联明星
	 */
	private function deleteArticleStar($articleId){
		 $this->query('delete from `ArticleStar` where ArticleId = %s', array($articleId));
	}
	
	/**
	 * 删除文章关联品牌
	 */
	private function deleteArticleBrand($articleId){
		 $this->query('delete from `ArticleBrand` where ArticleId = %s', array($articleId));
	}
	
	/**
	 * 删除文章关联服饰
	 */
	private function deleteArticleFashion($articleId){
		$this->query('delete from `ArticleFashion` where ArticleId = %s', array($articleId));
	}
	
	/**
	 * 删除文章关联地表
	 */
	private function deleteArticleArea($articleId){
		$this->query('delete from `ArticleArea` where ArticleId = %s', array($articleId));
	}
	
	/**
	 * 删除文章关联事件
	 */
	private function deleteArticleEvent($articleId){
		$this->query('delete from `ArticleEvent` where ArticleId = %s', array($articleId));
	}
	
	/**
	 * 删除文章关联图片 
	 */
	public function deleteArticleImage($articleId){
		$this->query('delete from `ArticleImage` where ArticleId = %s', array($articleId));
	}
	
	/**
	 * 删除文章关联标签
	 */
	public function deleteArticleRelatedTag($articleId,$tagType){
		$this->query('delete from `ArticleRelatedTag` where ArticleId = %s and TagType = %s', array($articleId,$tagType));
	}
	
	/**
	 * 根据文章ID查询图片列表
	 */
	public function getArticleImage($articleId){
	    $result = $this->query('select * from ArticleImage where ArticleId=%s', array($articleId));
	    
	    $retArr = array();
	    if($result){
    	    while($row = $result->fetch_assoc()){
    	        array_push($retArr, $row);
    	    }
	    }
	    return $retArr;
	}
	
	/**
	 * 根据文章ID查询分类
	 */
	public function getArticleColumn($articleId){
	    $retArr = '';
	    $result = $this->query('select b.Id,b.Name from ArticleColumn as a left join ColumnCategory as b on a.ColumnId=b.Id where a.ArticleId=%s', array($articleId));
	    if($result){
	        $retArr = $result->fetch_assoc();
	    }
	    return $retArr;
	}
	
	/**
	 * 根据文章ID查询投票列表
	 */
	public function getArticleVote($articleId){
	    $result = $this->query('select * from ArticleVote where ArticleId=%s', array($articleId));
	    
	    $retArr = array();
	    if($result){
    	    while($row = $result->fetch_assoc()){
    	        array_push($retArr, $row);
    	    }
	    }
	    return $retArr;
	}
	
	/**
	 * 根据文章ID查询关联明星
	 */
	public function getStarByArticleId($articleId){
	    $result = $this->query('select * from ArticleStar where ArticleId=%s', array($articleId));
	    
	    $retArr = array();
	    if($result){
    	    while ($row = $result->fetch_assoc()){
    	        $starData = new \haibao\cms\data\Celebrity();
    	        $starObj = $starData->getCelebrityById($result->getData('StarId'));
    	        if( isset($starObj->NameCn) ){
    	            $row['StarName'] = $starObj->NameCn;
    	            $row['StarNameEn'] = $starObj->NameEn;
    	        }
    	        array_push($retArr, $row);
    	    }
	    }
	    return $retArr;
	}
	
	/**
	 * 根据文章ID查询关联品牌
	 */
	public function getBrandByArticleId($articleId){
	    $result = $this->query('select BrandId from ArticleBrand where ArticleId=%s', array($articleId));
	    
	    $retArr = array();
	    if($result){
    	    while ($row = $result->fetch_assoc()){
    	        $brandData = new \haibao\cms\data\Brand();
    	        $brandObj = $brandData->getBrandById($result->getData('BrandId'));
    	        if( isset($brandObj->NameCN) ){
    	            $row['BrandName'] = $brandObj->NameCN;
    	            $row['BrandNameEN'] = $brandObj->NameEN;
    	        }
    	    
    	        array_push($retArr, $row);
    	    }
	    }
	    return $retArr;
	}
	
	/**
	 * 根据文章ID查询关联服饰
	 */
	public function getFashionByArticleId($articleId){
	    $result = $this->query('select b.Id,b.NameCN from ArticleFashion as a left join FashionElement as b on a.FashionId=b.Id where ArticleId=%s', array($articleId));
	    
	    $retArr = array();
	    if($result){
    	    while ($row = $result->fetch_assoc()) {
    	        array_push($retArr, $row);
    	    }
	    }
	    return $retArr;
	}
	
	/**
	 * 根据文章ID查询关联地标
	 */
	public function getAreaByArticleId($articleId){
	    $result = $this->query('select b.Id,b.Name from ArticleArea as a left join Area as b on a.AreaId=b.Id where ArticleId=%s', array($articleId));
	    
	    $retArr = array();
	    if($result){
    	    while ($row = $result->fetch_assoc()){
    	        array_push($retArr, $row);
    	    }
	    }
	    return $retArr;
	}
	
	/**
	 * 根据文章ID查询关联事件
	 */
	public function getEventByArticleId($articleId){
	    $result = $this->query('select EventId from ArticleEvent where ArticleId=%s', array($articleId));
	     
	    $retArr = array();
	    if($result){
    	    while ($row = $result->fetch_assoc()){
    	        $eventData = new \haibao\cms\data\Event();
    	        $eventObj = $eventData->getEventById($result->getData('EventId'));
    	        if( isset($eventObj->EventName) ){
    	            $row['EventName'] = $eventObj->EventName;
    	        }
    	        
    	        array_push($retArr, $row);
    	    }
	    }
	    return $retArr;
	}
	
	/**
	 * 根据文章ID查询关联标签
	 */
	public function getRelatedTagByArticleId($articleId,$type){
		$result = $this->query('select TagId from ArticleRelatedTag where ArticleId=%s and TagType = %s', array($articleId,$type));
		 
		$retArr = array();
		$str = null;
		if($result){
			while ($row = $result->fetch_assoc()){
				$str .= implode(',',$row).',';
			}
		}
		return  array_filter(explode(',',$str));
	}
}